package com.google.code.sip.ucenter.web.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.CreditRule;
import com.google.code.sip.ucenter.model.CreditRuleAmount;
import com.google.code.sip.ucenter.model.CreditRuleCycleType;
import com.google.code.sip.ucenter.model.CreditType;
import com.google.code.sip.ucenter.service.CreditRuleManager;
import com.google.code.sip.ucenter.service.CreditTypeManager;

/**
 * 积分规则
 * 
 * @author fengcai Jun 25, 2010 11:20:10 AM
 */

@Controller
public class CreditRuleController {

	@Autowired
	private CreditRuleManager creditRuleManager;

	@Autowired
	private CreditTypeManager creditTypeManager;

	@RequestMapping("/admin/creditRule!list.do")
	public ModelAndView list(HttpServletRequest request) {
		Page<CreditRule> page = new Page<CreditRule>();
		WebGridUtils.fillPage(request, page);
		creditRuleManager.getAllCreditRules(page);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("totalRows", (int) page.getTotalCount());
		model.put("page", page);
		// 获取积分类型
		List<CreditType> creditTypeList = creditTypeManager.getAllCreditTypes();
		model.put("creditTypeList", creditTypeList);

		return new ModelAndView("admin.creditRule.list", model);
	}

	@RequestMapping("/admin/creditRule!input.do")
	public ModelAndView input() {
		// 获取积分类型
		Map<String, Object> model = new HashMap<String, Object>();
		List<CreditType> creditTypeList = creditTypeManager.getAllCreditTypes();
		model.put("creditTypeList", creditTypeList);
		return new ModelAndView("admin.creditRule.input", model);
	}

	@RequestMapping("/admin/creditRule!save.do")
	public ModelAndView save(
			@ModelAttribute("CreditRule") CreditRule creditRule,
			@RequestParam("cycle") String cycle, HttpServletRequest request) {

		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map<String, Object> paramMap = WebUtils.getParametersStartingWith(
				request, "amout_");
		for (Map.Entry<String, Object> entity : paramMap.entrySet()) {
			String[] keyArr = entity.getKey().split("_");
			if (keyArr.length < 2) {
				CreditType creditType = creditTypeManager
						.getCreditType(keyArr[0]);
				CreditRuleAmount creditRuleAmount = new CreditRuleAmount();
				creditRuleAmount.setCreditType(creditType);
				creditRuleAmount.setAmout(Integer.valueOf(entity.getValue()
						.toString()));
				creditRule.getCreditRuleAmounts().add(creditRuleAmount);
			}
		}
		// 判断周期类型
		if (cycle.equals("0")) {
			creditRule.setCycleType(CreditRuleCycleType.One);
		}
		if (cycle.equals("1")) {
			creditRule.setCycleType(CreditRuleCycleType.EveryDay);
		}
		if (cycle.equals("2")) {
			creditRule.setCycleType(CreditRuleCycleType.NotLimit);
		}

		creditRuleManager.saveCreditRule(creditRule);
		return list(request);
	}

	@RequestMapping("/admin/creditRule!edit.do")
	public ModelAndView edit(@RequestParam("id") String id) {
		Map<String, Object> model = new HashMap<String, Object>();
		// 获取积分类型
		List<CreditType> creditTypeList = creditTypeManager.getAllCreditTypes();
		model.put("creditTypeList", creditTypeList);
		CreditRule creditRule = creditRuleManager.getCreditRule(id);
		model.put("creditRule", creditRule);
		return new ModelAndView("admin.creditRule.input", model);
	}
}
